#include "widget.h"
#include "ui_widget.h"

#include "e_grbdef.h"
#include "g_url.h"

#include "e_metdlg.h"
#include "e_notedlg.h"
#include "e_vokdlg.h"
#include "e_helpdlg.h"


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lbl_msgBar->installEventFilter(this);
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->lbl_body,&Widget::customContextMenuRequested,this,&Widget::showMenu);
    homePath=QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    hostName=QHostInfo::localHostName();
    today=date.currentDate().toString();
    pid=QCoreApplication::applicationPid();
    rid=QUuid::createUuid().toString();
    masName="用户";
    createRif();
    grbLanuch();
}

void Widget::showMenu()
{
    menuSetup();
    mo->exec(QCursor::pos());
}

void Widget::grbLanuch()
{
    if((!QFile::exists(homePath+e_lc)&&(!QFile::exists(e_rc))))
    {
        _greet();
        ercInit();
    }
    else if((!QFile::exists(homePath+e_lc))&&(QFile::exists(e_rc)))
    {
        _greet();
    }
    else if((QFile::exists(homePath+e_lc))&&(!QFile::exists(e_rc)))
    {
        uMode=u_user;
        elcRead();
        ercWrite();
        exp_sml;
        say("欢迎回来，"+masName+masHonor+"，出现了一点信息丢失，不过总体来说还不错~");
    }
    else if((QFile::exists(homePath+e_lc))&&(QFile::exists(e_rc)))
    {
        ercRead();
        greet();
    }
}
void Widget::ercInit()
{
    grbUid=QUuid::createUuid().toString();
    masUid=QUuid::createUuid().toString();
    uMode=u_user;
    QSettings set(e_rc,QSettings::IniFormat,this);
    set.setIniCodec("UTF-8");
    set.setValue("essant/userMode",uMode);
    set.setValue("essant/hostName",hostName);
    set.setValue("essant/homePath",homePath);
    set.setValue("essant/myName","绿坝子");
    set.setValue("essant/myUid",grbUid);
    set.setValue("essant/masUid",masUid);
}

void Widget::createRif()
{
    QSettings set("../var/run/"+rid,QSettings::IniFormat,this);
    set.setIniCodec("UTF-8");
    set.setValue("Session/rid",rid);
    set.setValue("Session/pid",pid);
}

void Widget::ercWrite()
{
    QSettings set(e_rc,QSettings::IniFormat,this);
    set.setIniCodec("UTF-8");
    set.setValue("essant/userMode",uMode);
    set.setValue("essant/hostName",hostName);
    set.setValue("essant/homePath",homePath);
    set.setValue("essant/myName",grbName);
    set.setValue("essant/myUid",grbUid);
    set.setValue("essant/masName",masName);
    set.setValue("essant/masHonor",masHonor);
    set.setValue("essant/masUid",masUid);
}

void Widget::ercRead()
{
    QSettings set(e_rc,QSettings::IniFormat,this);
    set.setIniCodec("UTF-8");
    grbName=set.value("essant/myName").toString();
    masName=set.value("essant/masName").toString();
    masHonor=set.value("essant/masHonor").toString();
}

void Widget::uidRead()
{
    QSettings set(e_rc,QSettings::IniFormat,this);
    set.setIniCodec("UTF-8");
    uMode=set.value("essant/userMode").toInt();
    grbUid=set.value("essant/myUid").toString();
    masUid=set.value("essant/masUid").toString();
}
void Widget::elcWrite()
{
    QFile file(homePath + e_lc);
    if(file.open(QIODevice::WriteOnly|QIODevice::Text))
    {
        QXmlStreamWriter gwr(&file);
        gwr.setAutoFormatting(true);
        gwr.writeStartDocument();
        gwr.writeStartElement("绿坝娘本地用户信息文件");
        switch(uMode)
        {
        case u_root:
            gwr.writeAttribute("用户模式", "root");break;
        case u_master:
            gwr.writeAttribute("用户模式", "master");break;
        case u_user:
            gwr.writeAttribute("用户模式", "user");break;
        case u_friend:
            gwr.writeAttribute("用户模式", "friend");break;
        case u_others:
            gwr.writeAttribute("用户模式", "others");break;
        case u_custom:
            gwr.writeAttribute("用户模式", "custom");break;
        case u_blacklist:
            gwr.writeAttribute("用户模式", "blacklist");break;
        default:
            gwr.writeAttribute("用户模式", "error");break;
        }
        gwr.writeTextElement("主人的计算机名", hostName);
        gwr.writeTextElement("主人的主目录", homePath);
        gwr.writeTextElement("我的名字",grbName);
        gwr.writeTextElement("我的ID",grbUid);
        gwr.writeTextElement("主人的名字", masName);
        gwr.writeTextElement("主人的称呼", masHonor);
        gwr.writeTextElement("主人的ID", masUid);
        gwr.writeEndElement();
        gwr.writeEndDocument();
        file.close();
    }
}

void Widget::elcRead()
{
    QFile file(homePath + e_lc);
    if(file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        QXmlStreamReader gr(&file);
        gr.readNext();
        while(!gr.atEnd())
        {
            gr.readNext();
            if(gr.isStartElement())
            {
                if(gr.name()=="我的名字")
                {
                    grbName=gr.readElementText();
                }
                if(gr.name()=="我的ID")
                {
                    grbUid=gr.readElementText();
                }
                if(gr.name()=="主人的名字")
                {
                    masName=gr.readElementText();
                }
                if(gr.name()=="主人的称呼")
                {
                    masHonor=gr.readElementText();
                }
                if(gr.name()=="主人的ID")
                {
                    masUid=gr.readElementText();
                }
            }
        }
        file.close();
    }
}

void Widget::meet()
{
    sl;
    if(!QFile::exists(homePath+e_lc))
    {
        firstMeet();
    }
    else
    {
        secondMeet();
    }
}

void Widget::firstMeet()
{
    e_metdlg met;
    switch(met.exec())
    {
    case QDialog::Accepted:
    {
        masName=met.masName;
        masHonor=met.masHonor;
        grbName=met.grbName;
        uidRead();
        ercWrite();
        elcWrite();
        exp_sml;
        say("好的，那么往后就用" + masName + masHonor + "来称呼哦");
        v42;
        delay(6);
        exp_nml;
        sl;
    }
        break;
    case QDialog::Rejected:
    {
        exp_akd;
        say("嘛嘛，不告诉人家就算");
        delay(3);
        exp_nml;
        sl;
    }
        break;
    }
}

void Widget::secondMeet()
{
    e_metdlg met;
    switch(met.exec())
    {
    case QDialog::Accepted:
    {
        masNewName=met.masName;
        masNewHonor=met.masHonor;
        grbName=met.grbName;
        if(masName==masNewName&&masHonor==masNewHonor)
        {
            exp_srp;
            say("好的，那么就跟目前一样用"+masName+ masHonor+"来称呼哦");
            v43;
            delay(7);
            sl;
        }
        else
        {
            masName=masNewName;
            masHonor=masNewHonor;
            uidRead();
            ercWrite();
            elcWrite();
            exp_sml;
            say("好的，那么往后就用" + masName + masHonor + "来称呼哦");
            v42;
            delay(5);
            exp_nml;
            sl;
        }
    }
        break;
    case QDialog::Rejected:
    {
        exp_srp;
        say("不用改变也可以吗？");
        v41;
        delay(3);
        sl;
    }
        break;
    }
}

void Widget::greet()
{
    curTime=time.currentTime().toString();
    hour=time.currentTime().hour();
    if(hour >=4 && hour <6)
    {
        say(masName+masHonor+"，你起的好早啊");
        v2;
    }

    if(hour>=6 && hour<12)
    {
        say("早上好，"+masName+masHonor);
        v3;
    }
    if(hour>=12 && hour<13)
    {
        say("中午好，"+masName+masHonor);
        v4;
    }
    if(hour>=13 && hour <20)
    {
        say("下午好，"+masName+masHonor);
        v6;
    }
    if(hour>=20 && hour<23)
    {
        say("晚上好，"+ masName+masHonor);
        v9;
    }
    if(hour == 23)
    {
        say("半夜了呢，" + masName+masHonor);
        v23;
    }
    if(hour >=0 && hour <4)
    {
        say("半夜了呢，"+ masName+masHonor+"，不要看什么工口网站啊！");
        v23;
    }
}

void Widget::_greet()
{
    curTime=time.currentTime().toString();
    hour=time.currentTime().hour();
    if(hour >=4 && hour <6)
    {
        say("主人，你起的好早啊");
        v2;
    }

    if(hour>=6 && hour<12)
    {
        say("早上好，主人");
        v3;
    }
    if(hour>=12 && hour<13)
    {
        say("中午好，主人");
        v4;
    }
    if(hour>=13 && hour <20)
    {
        say("下午好，主人");
        v6;
    }
    if(hour>=20 && hour<23)
    {
        say("晚上好，主人");
        v9;
    }
    if(hour == 23)
    {
        say("半夜了呢，主人");
        v23;
    }
    if(hour >=0 && hour <4)
    {
        say("半夜了呢，主人，不要看什么工口网站啊！");
        v9;;
    }
}
void Widget::delay(int sec)
{
    time.start();
    while(time.elapsed()<sec*1000)
        qApp->processEvents();
}

void Widget::exitGrb()
{
    exitOpt();
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    winPos=this->pos();
    mosPos=event->globalPos();
    dPos=mosPos - winPos;
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - dPos );
}

bool Widget::eventFilter(QObject *obj, QEvent *event)
{
    if(obj==ui->lbl_msgBar&&event->type()==QEvent::MouseButtonDblClick)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case Qt::Key_Escape:
        exp_dst;
        exitQuickOpt();
        break;

    case Qt::Key_Space:
        menuSetup();
        mo->exec(QCursor::pos());
        break;

    case Qt::Key_F1:
        showHelp();
        break;

    case Qt::Key_F2:
        quickNote();
        break;

    case Qt::Key_F4:
        quickInvoke();
        break;
    }
}

void Widget::exitQuickOpt()
{
    v12f;
    sl;
    QMessageBox::StandardButton msg=QMessageBox::question(this,"您按下了逃跑按钮","要出去了吗？",QMessageBox::Yes|QMessageBox::Cancel);
    if(msg==QMessageBox::Yes)
    {
        qApp->quit();
    }
    else
    {
        v85;
        exp_sml;
        say("那我们就继续玩吧");
        delay(4);
        sl;
    }

}
void Widget::exitOpt()
{
    v12f;
    QMessageBox::StandardButton msg=QMessageBox::question(this,"要出去了吗","要出去了吗？",QMessageBox::Yes|QMessageBox::Cancel);

    if(msg==QMessageBox::Yes)
    {
        v10f;
        exp_rge;
        say("可恶，别以为关掉我就不能监视你了！");
        delay(4);
        qApp->quit();
    }
    else
    {
        v85;
        exp_sml;
        say("那我们就继续玩吧");
        delay(4);
        sl;
    }
}

/*void Widget::contextMenuEvent(QContextMenuEvent *event)
{
    menuSetup();
    mo->exec(event->globalPos());
}
*/

void Widget::showHelp()
{
    sl;
    e_helpdlg help;
    help.exec();
}

void Widget::mouseDoubleClickEvent(QMouseEvent *)
{
    v14;
    exp_arh;
    say("啊！不要啊~");
    delay(4);
    sl;
    exp_mif;
}

void Widget::showTime()
{
    curTime=time.currentTime().toString();
    exp_akd;
    say("现在时间是："+ curTime);
    delay(3);
    exp_nml;
    sl;
}

void Widget::about()
{
    sl;
    exp_sml;
    QMessageBox::information(this,"关于我","我现在只是一个小小的shell demo >_<",QMessageBox::Ok);
}

void Widget::aboutGrb()
{
    sl;
    exp_akd;
    QMessageBox::information(this,"绿坝子自述","绿坝娘（英语：Green Dam Girl，日语：グリーンダムたん，Green Dam Tan）是对中国大陆网络过滤软件《绿坝-花季护航》的萌拟人化产物。\n中国大陆的ACG爱好者们用这样一种调侃的方式表达了对政府大规模推广绿坝软件这一有妨碍信息畅顺通达之嫌的行为的不满。\n目前还没有一致公认的人物造型，但性格上基本已经确认为傲娇加天然呆。",QMessageBox::Ok);
    delay(2);
    exp_nml;
}
void Widget::aboutQt()
{
    exp_nml;
    QMessageBox::aboutQt(this,"关于Qt");
    delay(2);
    sl;
}

void Widget::exitApp()
{
    qApp->quit();
}

void Widget::quickInvoke()
{
    sl;
    e_vokdlg vok;
    vok.exec();
}

void Widget::viewSysVer()
{
    winver=QSysInfo::windowsVersion();

    if(winver==QSysInfo::WV_4_0)
    {
        say("您的操作系统：\n操作系统版本4.0，对应到Windows NT");
    }
    if(winver==QSysInfo::WV_5_0)
    {
        say("您的操作系统：\n操作系统版本5.0，对应到Windows 2000");
    }
    if(winver==QSysInfo::WV_5_1)
    {
        say("您的操作系统：\n操作系统版本5.1，对应到Windows XP");
    }
    if(winver==QSysInfo::WV_5_2)
    {
        say("您的操作系统：\n操作系统版本5.2，对应到Windows 2003");
    }
    if(winver==QSysInfo::WV_6_0)
    {
        say("您的操作系统：\n操作系统版本6.0，对应到Windows Vista与Windows 2008");
    }
    if(winver==QSysInfo::WV_6_1	)
    {
        say("您的操作系统：\n操作系统版本6.1，对应到Windows 7与Windows 2008 R2");
    }
    if(winver==QSysInfo::WV_6_2)
    {
        say("您的操作系统：\n操作系统版本6.2，对应到Windows 8与Windows 2012");
    }

    delay(4);
    sl;
}

void Widget::quickNote()
{
    sl;
    exp_nml;
    e_notedlg note;
    note.exec();
}

void Widget::quickTest()
{

}

void Widget::visitBaidu()
{
    QDesktopServices::openUrl(url_baidu);
}
void Widget::visitGoogle()
{
    QDesktopServices::openUrl(url_google);
}

void Widget::actionSetup()
{

    showTimeAction=new QAction("报时",this);
    connect(showTimeAction,&QAction::triggered,this,&Widget::showTime);

    aboutAction=new QAction("关于本软件",this);
    connect(aboutAction,&QAction::triggered,this,&Widget::about);

    aboutQtAction=new QAction("关于Qt",this);
    connect(aboutQtAction,&QAction::triggered,this,&Widget::aboutQt);

    aboutGrbAction=new QAction("关于绿坝子",this);
    connect(aboutGrbAction,&QAction::triggered,this,&Widget::aboutGrb);

    exitAction=new QAction("退出",this);
    connect(exitAction,&QAction::triggered,this,&Widget::exitGrb);

    exitAppAction=new QAction("直接退出(ESC)",this);
    connect(exitAppAction,&QAction::triggered,this,&Widget::exitApp);

    quickInvokeAction=new QAction("快捷操作(F6)",this);
    connect(quickInvokeAction,&QAction::triggered,this,&Widget::quickInvoke);

    quickTestAction=new QAction("测试用",this);
    connect(quickTestAction,&QAction::triggered,this,&Widget::quickTest);

    visitBaiduAction=new QAction("百度",this);
    connect(visitBaiduAction,&QAction::triggered,this,&Widget::visitBaidu);

    visitGoogleAction=new QAction("谷歌",this);
    connect(visitGoogleAction,&QAction::triggered,this,&Widget::visitGoogle);

    viewSysVerAction=new QAction("系统版本",this);
    connect(viewSysVerAction,&QAction::triggered,this,&Widget::viewSysVer);

    meetAction=new QAction("主人的名字",this);
    connect(meetAction,&QAction::triggered,this,&Widget::meet);

    noteAction=new QAction("速记(F2)",this);
    connect(noteAction,&QAction::triggered,this,&Widget::quickNote);

    helpAction=new QAction("帮助(F1)",this);
    connect(helpAction,&QAction::triggered,this,&Widget::showHelp);
}

void Widget::menuSetup()
{
    actionSetup();
    mo=new QMenu(this);
    m_user=new QMenu(masName + "大人",this);
    m_util=new QMenu("工具",this);
    m_sys=new QMenu("系统",this);
    m_info=new QMenu("信息",this);
    m_grb=new QMenu("绿坝子",this);
    m_dev=new QMenu("开发用",this);

    m_grb_note=new QMenu("绿坝子的收藏夹",this);
    m_grb_xin=new QMenu("里世界",this);
    m_grb_url_com=new QMenu("常用网站",this);
    m_grb_url_quick=new QMenu("快速访问",this);
    m_grb_url_spec=new QMenu("和谐号",this);

    m_user->addAction(meetAction);

    m_sys->addAction(quickInvokeAction);

    m_util->addAction(noteAction);
    m_util->addAction(showTimeAction);

    m_info->addAction(viewSysVerAction);
    m_info->addSeparator();
    m_info->addAction(aboutAction);
    m_info->addAction(aboutQtAction);

    m_grb->addMenu(m_grb_note);
    m_grb->addMenu(m_grb_xin);
    m_grb->addSeparator();
    m_grb->addAction(aboutGrbAction);
    m_grb->addAction(exitAppAction);

    m_grb_note->addMenu(m_grb_url_quick);
    m_grb_note->addMenu(m_grb_url_com);

    m_grb_xin->addMenu(m_grb_url_spec);

    m_grb_url_quick->addAction(visitBaiduAction);
    m_grb_url_quick->addAction(visitGoogleAction);

    m_dev->addAction(quickTestAction);

    mo->addMenu(m_user);
    mo->addMenu(m_util);
    mo->addMenu(m_sys);
    mo->addMenu(m_info);
    mo->addMenu(m_grb);
    mo->addSeparator();
    mo->addMenu(m_dev);
    mo->addAction(helpAction);
    mo->addAction(exitAction);
}

Widget::~Widget()
{
    delete ui;
}

